package sim1011.examples.kame.test;

import jade.core.Agent;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;

public class SmallBrainAgent extends Agent {
	private static final long serialVersionUID = 5988112666275278525L;
	public AgentContainer pl0,pl1;
	public ContainerProperties prop0, prop1;
	private AgentController[] agentC0 = new AgentController[10];
	private AgentController[] agentC1 = new AgentController[10];
	
	public void setup() {
		System.out.println("started setup " + getAID().getName());
		addBehaviour(new ReceiveMessageBehaviour());
		// register brain service
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType("sim-type");
		sd.setName("sim-small-brain");
		dfd.addServices(sd);
		try {
			DFService.register(this, dfd);
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
		// nodes
		Runtime t = Runtime.instance();
		Profile p0 = new ProfileImpl();
		p0.setParameter(Profile.LOCAL_HOST, "localhost");
		p0.setParameter(Profile.PLATFORM_ID, "sim-platform");
		p0.setParameter(Profile.CONTAINER_NAME, "fixedNode.0");
		pl0 = (AgentContainer) t.createAgentContainer(p0);
		Profile p1 = new ProfileImpl();
		p1.setParameter(Profile.LOCAL_HOST, "localhost");
		p1.setParameter(Profile.PLATFORM_ID, "sim-platform");
		p1.setParameter(Profile.CONTAINER_NAME, "mobileNode.0");
		pl1 = (AgentContainer) t.createAgentContainer(p1);
		prop0 = new ContainerProperties(1000, 2048, 10, 15, false);
		prop1 = new MobileContainerProperties(200, 32, 40, 60, 100);
		try {
			System.out.println("Created: " + pl0.getContainerName());
			System.out.println("Created: " + pl0.getPlatformName());
			System.out.println("Created: " + pl1.getContainerName());
			System.out.println("Created: " + pl1.getPlatformName());
			// create components
			Object[] p0Prop = {this, prop0};
			Object[] p1Prop = {this, prop1};
			for (int i=0; i<agentC0.length; ++i) {
				agentC0[i] = pl0.createNewAgent("agent0." + i, "sim1011.examples.kame.test.Component", p0Prop);
				agentC1[i] = pl0.createNewAgent("agent1." + i, "sim1011.examples.kame.test.Component", p1Prop);
				agentC0[i].start();
				agentC1[i].start();
			}
		} catch (StaleProxyException e) {
			e.printStackTrace();
		} catch (ControllerException e) {
			e.printStackTrace();
		}
	}
	
	protected void takeDown() {
		try {
			DFService.deregister(this);
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}

	public synchronized Object getCurrentProperties(String content) {
		try {
			if (content.equals(pl1.getContainerName()))
				return prop1;
			else
				return prop0;
		} catch (ControllerException e) {
			e.printStackTrace();
		}
		return null;
	}
}

class ReceiveMessageBehaviour extends CyclicBehaviour {
	private static final long serialVersionUID = -3983246396296508379L;

	@Override
	public void action() {
		MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
		ACLMessage msg = myAgent.receive(mt);
		if (msg != null) {
			System.out.println(((SmallBrainAgent)myAgent).prop0.getCPU());
			System.out.println(((SmallBrainAgent)myAgent).prop0.getMEM());
			if (msg.getContent().contains("Request where to execute")) {
				ACLMessage rep = msg.createReply();
				rep.setPerformative(ACLMessage.INFORM);
				try {
					rep.setContent(((SmallBrainAgent)myAgent).pl1.getContainerName());
				} catch (ControllerException e) {
					e.printStackTrace();
				}
				myAgent.send(rep);
			}
		} else {
			block();
		}
	}
}
